package com.xjtu.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xjtu.dto.UserPageDto;
import com.xjtu.dto.care.AddCareRecordDto;
import com.xjtu.dto.care.UpdateCareRecordDto;
import com.xjtu.entity.Elderly;
import com.xjtu.entity.ElderlyCareRecord;
import com.xjtu.entity.ServiceRecord;
import com.xjtu.entity.User;
import com.xjtu.exception.BaseException;
import com.xjtu.mapper.ElderlyCareRecordMapper;
import com.xjtu.mapper.ElderlyMapper;
import com.xjtu.result.PageResult;
import com.xjtu.service.CareRecordService;
import com.xjtu.vo.careRecord.CareRecordVo;
import com.xjtu.vo.serviceRecord.ServiceRecordVo;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class CareRecordServiceImpl implements CareRecordService {

    @Autowired(required = false)
    private ElderlyMapper elderlyMapper;

    @Autowired(required = false)
    private ElderlyCareRecordMapper elderlyCareRecordMapper;
    @Override
    public void save(AddCareRecordDto addCareRecordDto) {
        // 首先判断老人是否存在
        Elderly elderly = elderlyMapper.selectByPrimaryKey(Long.valueOf(addCareRecordDto.getElderlyId()));
        if(elderly == null){
            throw new BaseException("老人不存在，检查参数");
        }
        ElderlyCareRecord elderlyCareRecord = new ElderlyCareRecord();
        BeanUtils.copyProperties(addCareRecordDto,elderlyCareRecord);
        elderlyCareRecord.setRecordTime(LocalDateTime.now());
        elderlyCareRecordMapper.insert(elderlyCareRecord);
    }

    @Override
    public void update(UpdateCareRecordDto updateCareRecordDto) {
        // 首先判断id是否存在
        ElderlyCareRecord elderlyCareRecord = elderlyCareRecordMapper.selectByPrimaryKey(Long.valueOf(updateCareRecordDto.getRecordId()));
        if(elderlyCareRecord == null){
            throw new BaseException("记录不存在，检查记录id");
        }
        ElderlyCareRecord record = new ElderlyCareRecord();
        BeanUtils.copyProperties(updateCareRecordDto,record);
        elderlyCareRecordMapper.updateByPrimaryKey(record);
    }

    @Override
    public void delete(Integer id) {
        elderlyCareRecordMapper.deleteByPrimaryKey(Long.valueOf(id));
    }

    @Override
    public CareRecordVo queryById(Integer id) {
        ElderlyCareRecord record = elderlyCareRecordMapper.selectByPrimaryKey(Long.valueOf(id));
        if(record == null){
            throw new BaseException("记录不存在");
        }
        Elderly elderly = elderlyMapper.selectByPrimaryKey(Long.valueOf(record.getElderlyId()));
        CareRecordVo careRecordVo = new CareRecordVo();
        BeanUtils.copyProperties(record,careRecordVo);
        careRecordVo.setElderlyName(elderly.getName());
        return careRecordVo;
    }

    @Override
    public PageResult pageQuery(Integer page, Integer size, String name) {
        PageHelper.startPage(page, size);
        UserPageDto userPageDto = new UserPageDto();
        userPageDto.setPage(page);
        userPageDto.setSize(size);
        userPageDto.setName(name);
        log.info("传入的参数为{}",userPageDto);
        log.info("要查找的名字为{}",name);
        Page<ElderlyCareRecord> userPage = elderlyCareRecordMapper.pageQuery(name);
        long total = userPage.getTotal();
        List<ElderlyCareRecord> records = userPage.getResult();
        List<CareRecordVo> respLists = new ArrayList<>();
        for(ElderlyCareRecord record : records){
            Elderly elderly = elderlyMapper.selectByPrimaryKey(Long.valueOf(record.getElderlyId()));
            // 进行返回
            CareRecordVo resp = new CareRecordVo();
            BeanUtils.copyProperties(record,resp);
            resp.setElderlyName(elderly.getName());
            respLists.add(resp);
        }
        return new PageResult(total, respLists);
    }
}
